package com.woniu.servicepay.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class PayMQConfig {
    //主队列和交换机
    final static public String PAY_EXCHANGE = "pay.exchange";
    final static public String PAY_QUEUE= "pay.queue";
    //死信队列和交换机
    final static public String DLX_EXCHANGE = "pay.dlx.exchange";
    final static public String DLX_QUEUE = "pay.dlx.queue";
    final static public String DLX_PAY_KEY = "pay.dlx";
    // 最大重试次数
    public static final int MAX_RETRY_COUNT = 3;

    @Bean
    public Queue payQueue() {
        //绑定死信队列
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", DLX_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", DLX_PAY_KEY);
        return new Queue(PAY_QUEUE,true,false,false,arguments);
    }
    @Bean
    public DirectExchange payExchange() {
        return new DirectExchange(PAY_EXCHANGE);
    }
    @Bean
    public Binding payBinding() {
        return BindingBuilder.bind(payQueue()).to(payExchange()).with("payment.handling");
    }
    @Bean
    public Queue dlxQueue() {
        return new Queue(DLX_QUEUE,true);
    }
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE);
    }
    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(DLX_PAY_KEY);
    }


}
